/**
 * 
 * author：gaoxingliang@outlook.com
 * created:2015年8月15日 上午10:42:57  
 */
package org.gxl.learn;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * 使用POI生成excel文件
 * @author gxl
 *
 */
public class WriteExcelByPOI
{

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		// TODO Auto-generated method stub
		HSSFWorkbook w = new HSSFWorkbook();
		HSSFSheet sheet = w.createSheet();
		//从0开始
		HSSFRow r = sheet.createRow(0);
		HSSFCell cell = null;
		
		String [] title = {"id", "name"};
		//表头
		for (int i=0; i< title.length; i++)
		{
			cell = r.createCell(i);
			cell.setCellValue(title[i]);
		}
		//追加数据
		for (int i = 1; i<=10;i++)
		{
			HSSFRow row = sheet.createRow(i);
			HSSFCell idC = row.createCell(0);
			HSSFCell nameC = row.createCell(1);
			idC.setCellValue("id" + i);
			nameC.setCellValue("name" + i);
		}
		//创建文件
		File file = new File("poi.xls");
		file.createNewFile();
		w.write(new FileOutputStream(file));
		//stream should close
		w.close();
	}

}
